# =class Deck
#
# A deck of cards has 52 cards div?ded over 4 suits. When setting up a new
# instance of this class the 4 suits are initialized after which the 13 cards
# are added to each suit. Methods for getting an list of cards, the suits
# (holding cards) or getting a random card are provided for. Finally the +deal+
# method is used to generate a bridge deal (note: may have to refactor this
# when catering for other types of games)
class Deck

# setup the four suits and add 13 cards to each suit
def initialize()
@suits = [Suit.new("clubs"), Suit.new("diamonds"), Suit.new("hearts"),
Suit.new("spades")]

@suits.each{ |suit| %w{A K Q J T 9 8 7 6 5 4 3 2}.each{ |value| suit << Card.new(suit.name, value) }
}
end

# return all cards presently in the deck
def cards
return @suits.inject([]) { |list,suit| list += suit.cards }
end

# return all suits (which hold cards) presently in the deck
def suits
return @suits
end

# generate a bridge deal, consisting of 4 hands which contain cards
# consisting of 13 randomly selected cards from the deck. This only works if
# no cards have been removed from the deck yet.
def deal
if cards.size != 52
raise "not enough cards in the deck to create a deal"
exit
end

@hands = [Hand.new(), Hand.new(), Hand.new(), Hand.new()]
@hands.each{ |hand|
    13.times{
        hand << self.getRandomCard
    }
}

return @hands
end

# get and remove a random card (if any) from the deck
def getRandomCard
cards = self.cards
card = cards.slice( rand( cards.size) )
self.removeCard(card)
return card
end

# remove a card from the deck
def removeCard(card)
suit = @suits.select{ |s| s.name == card.suit}[0]
suit.removeCard(card)
end
end

if __FILE__ == $0
mg = Deck.new
mg.getRandomCard

end